package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam){
        log.info("查询学生信息, {}", studentQueryParam);
        PageResult<Student> pageStudent =  studentService.page(studentQueryParam);
        return Result.success(pageStudent);
    }
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除学生信息, {}", ids);
        studentService.delete(ids);
        return Result.success();
    }
    @PostMapping
    public Result save(@RequestBody Student student){
        try {
            log.info("添加学生信息, {}", student);
            studentService.save(student);
            return Result.success();
        } catch (Exception e) {
            log.error("添加学生信息失败, {}", student, e);
            return Result.error("添加学生信息失败");
        }
    }
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("更新学生信息, {}", student);
        studentService.update(student);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("查询学生信息, {}", id);
        Student student = studentService.getInfo(id);
        return Result.success(student);
    }
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolationCount(@PathVariable Integer id, @PathVariable Integer score){
        log.info("更新学生违纪信息, {} {}", id, score);
        studentService.updateViolationCount(id, score);
        return Result.success();
    }
}
